/**
 * Ansel 自定义select插件
 * v0.1
 * 3126620990
 */
(function ($) {
    $.fn.extend({
        anselcfg: function (obj) {
            if ($(this).length <= 0) {
                return false;
            }
            return $(this).each(function () {
                var _this = $(this), _val, _txt, isval = _this.attr('isval') || 'false',
                    msg = _this.attr('msg') || '选择数据';
                _this.hide().before('<div class="ansel_input"><input class="ansel_inputval" placeholder="' + msg + '" readonly></div>')
                _this.before('<div class="ansel_select">' +
                    '<div class="ansel_ul">' +
                    '</div>' +
                    // '<div class="ansel_btns">' +
                    // '<div class="ansel_btnss">' +
                    // // '<div class="ansel_btn ansel_ok">确定</div>' +
                    // // '<div class="ansel_btn ansel_no">关闭</div>' +
                    // // '<span class="ansel_no">关闭</span>' +
                    // '</div>' +
                    // '</div>' +
                    '</div>');
                var ansel_select = _this.prev(".ansel_select"),
                    ansel_ul = _this.prev(".ansel_select").children(".ansel_ul"),
                    ansel_inputval = ansel_select.prev(".ansel_input").children(".ansel_inputval"),
                    ansel_btns = ansel_select.find('.ansel_btns > .ansel_btnss'),
                    ansel_btn_ok = ansel_btns.children('.ansel_ok'),
                    ansel_btn_no = ansel_btns.children('.ansel_no'),
                    ansel_search_input = ansel_select.find('.ansel_search > input');
                ansel_ul.empty();
                _this.find("option").each(function (index, el) {
                    var _li = $('<div class="ansel_li" val="' + $(el).val() + '"><span class="ansel_check"></span>' + $(el).text() + '</div>');
                    if ($(el).prop("selected")) {
                        _li.children('.ansel_check').addClass("ansel_cur");
                        ansel_inputval.val($(el).text());
                    }
                    ansel_ul.append(_li);
                });
                ansel_ul.children(".ansel_li").click(function () {
                    $(this).parent().find('.ansel_check').removeClass('ansel_cur');
                    $(this).children('.ansel_check').addClass('ansel_cur');
                    _val = $(this).attr('val');
                    _txt = $(this).text();
                    _this.val(_val);
                    _this.trigger('change');
                    ansel_inputval.val(_txt);
                    ansel_select.hide(100);
                });
                ansel_btn_no.click(function () {
                    ansel_select.hide();
                });
                ansel_btn_ok.click(function () {
                    // if(isval=='true'){
                    //     _val=ansel_ul.children('.ansel_li').find('.ansel_cur').parent('.ansel_li').attr('val');
                    //     if(!_val){
                    //         return false;
                    //     }
                    // }
                    // _txt=ansel_ul.children('.ansel_li').find('.ansel_cur').parent('.ansel_li').text();
                    // _this.val(_val);
                    // ansel_inputval.val(_txt);
                    // ansel_select.hide(100);
                });
                ansel_select.prev(".ansel_input").click(function () {
                    $('.ansel_select').hide();
                    ansel_search_input.val('');
                    $(this).next().show();
                    ansel_ul.children(".ansel_li").show();
                    var m_height = ansel_select.height();
                    var h_height = 55;
                    var b_height = 49;
                    var c_height = ansel_select.height() - h_height - b_height;
                    // ansel_ul.css("height",c_height+"px");
                });



                ansel_search_input.on('input propertychange', function () {
                    var result = $.trim($(this).val());
                    if (result) {
                        ansel_ul.children(".ansel_li").hide().filter(":contains('" + result + "')").show();
                    } else {
                        ansel_ul.children(".ansel_li").show();
                    }
                });
            });
        }
    })
})(jQuery);

